# coding: utf-8

import os

from common.models import User
from common.utils import QiNiu

base_dir = os.path.dirname(os.path.dirname(__file__))


def get_avatar_users():
    return User.query.filter(
        User.avatar != ''
    ).all()


def upload_avatar():

    users = get_avatar_users()

    for user in users:
        if os.path.exists(user.avatar):
            with open(user.avatar, 'rb') as f:
                content = f.read()
                # FIXME by ld_wu 2018-03-07 key规则
                key = ''
                QiNiu().upload('avatar', key, content)
                user.avatar = key
